package org.ielse.frame.view;

import org.ielse.frame.App;
import org.ielse.frame.R;
import org.ielse.frame.util.NetworkUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;

public class ImageView extends android.widget.ImageView {

	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_ROUND = 1;
	public static final int TYPE_OVAL = 2;
	private int type = TYPE_NORMAL;

	private ImageLoader imageLoader;
	private ImageListener imageListener;

	public ImageView(Context context) {
		this(context, null);
	}

	public ImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		imageLoader = App.i().getImageLoader();
		imageListener = ImageLoader.getImageListener(this, R.drawable.test_img_loading, R.drawable.test_img_fail);
	}

	public void loadImage(final String url) {
		boolean downImageJustWifi = App.i().getAppSettings().isDownImageJustWifi();
		if (downImageJustWifi && !NetworkUtil.isGPRSOpened()) {
			imageLoader.get(url, imageListener);
		} else {
			setImageResource(R.drawable.test_img_default);
			setOnClickListener(new OnClickListener() {
				@Override public void onClick(View arg0) {
					imageLoader.get(url, imageListener);
				}
			});
		}
	}

	@Override public void setImageResource(int resId) {
		Log.d("else Image", "setImageResource");
		super.setImageResource(resId);
	}

	@Override public void setImageBitmap(Bitmap bm) {
		Log.d("else Image", "setImageBitmap");
		switch (type) {
		case TYPE_ROUND:
			bm = round(bm, bm.getWidth() / 10);
			break;
		case TYPE_OVAL:
			bm = oval(bm);
			break;
		}

		super.setImageBitmap(bm);

	}

	public static Bitmap round(Bitmap resource, float roundSize) {
		Bitmap output = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		final RectF rectF = new RectF(0, 0, resource.getWidth(), resource.getHeight());
		canvas.drawRoundRect(rectF, roundSize, roundSize, paint);
		paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(resource, 0, 0, paint);
		return output;
	}

	public static Bitmap oval(Bitmap resource) {
		Bitmap output = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		final RectF rectF = new RectF(0, 0, resource.getWidth(), resource.getHeight());
		canvas.drawOval(rectF, paint);
		paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(resource, 0, 0, paint);
		return output;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
}
